package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.domain.Address;
import com.qf.domain.Cart;
import com.qf.utils.DataSourceUtils;
import com.qf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/20 20:39
 * description:
 */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> findAddListByUid(int uid) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,detail,name,phone,uid,level from tb_address where uid=? ";
        try {
            List<Address> addressList=queryRunner.query(sql, new BeanListHandler<>(Address.class),uid);
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(Address address) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" insert into tb_address (id,detail,name,phone,uid,level) values(null,?,?,?,?,?)";
        try {
            queryRunner.update(sql,address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel() );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAddressByAid(String aid) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" delete from tb_address where id=? ";
        try {
            queryRunner.update(sql,aid );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Address address) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" update tb_address set detail=?,name=? ,phone=? ,uid=?,level=? where id=?";
        try {
            queryRunner.update(sql,address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel(),address.getId() );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void clearDefauletAddress(int uid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        QueryRunner queryRunner=new QueryRunner();
        String sql=" update tb_address set level=0 where uid=?";
        try {
            queryRunner.update(conn,sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setDefauletAddress(String aid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        QueryRunner queryRunner=new QueryRunner();
        String sql=" update tb_address set level=1 where id=?";
        try {
            queryRunner.update(conn,sql,aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
